using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private float speed = 5.6f;
    [SerializeField] private float jumpForce;
    [SerializeField] private float fallMultiplier; //空中降落的速度
    [SerializeField] private float lowJumpMultiplier; //跳跃高度的限制级别（数值越大跳的越矮）
    [SerializeField] private int jumpNum; //一共能跳几次
    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private PlayerAnimation playerAnimation;
    [SerializeField] private float rayPositionY = -0.5f; //射线的Y轴

    private Rigidbody2D rb;
    private float xVelocity; //这个变量主要来存储GetAxis或GetAxisRaw获取的值
    private bool pressJump;
    private int jumpRemainNum; //还能跳几次
    private bool isOnGround; //是否在地面上
    private float groundDistance = 0.2f; //射线的长度
    private float footOffset = 0.4f; //两条射线的距离

    // Start is called before the first frame update
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        pressJump = Input.GetButton("Jump");
        Jump();
    }

    void FixedUpdate()
    {
        PhysicsCheck();
        GroundMovement();
    }

    public void GroundMovement()
    {
        xVelocity = Input.GetAxisRaw("Horizontal");

        if (xVelocity != 0)
        {
            playerAnimation.PlayerRunBegin();
        }
        else
        {
            playerAnimation.PlayerRunEnd();
        }

        rb.velocity = new Vector2(xVelocity * speed, rb.velocity.y);
        if (transform.localScale.x != (xVelocity > 0 ? 1 : -1))
        {
            if (xVelocity < 0)
                transform.localScale = new Vector3(-1, 1, 0);
            if (xVelocity > 0)
                transform.localScale = new Vector3(1, 1, 0);
        }
    }

    void PhysicsCheck()
    {
        RaycastHit2D leftCheck =
            Raycast(new Vector2(-footOffset, rayPositionY), Vector2.down, groundDistance, groundLayer);
        RaycastHit2D RightCheck =
            Raycast(new Vector2(footOffset, rayPositionY), Vector2.down, groundDistance, groundLayer);
        if (leftCheck || RightCheck)
            isOnGround = true;
        else
            isOnGround = false;
    }

    void Jump()
    {
        if (Input.GetButtonDown("Jump"))
        {
            playerAnimation.PlayerJump();
            if (isOnGround)
            {
                jumpRemainNum = jumpNum;
            }

            if (pressJump && jumpRemainNum-- > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            }
        }

        if (rb.velocity.y < 0)//对下落的调整
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
        }
        else if (rb.velocity.y > 0 && !pressJump)//对跳跃的调整
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * lowJumpMultiplier * Time.deltaTime;
        }

        if (rb.velocity.y > 0)
        {
            playerAnimation.PlayerUp();
        }
        else if (rb.velocity.y < 0)
        {
            playerAnimation.PlayerDown();
        }else if (rb.velocity.y == 0)
        {
            playerAnimation.PlayerLand();
        }
    }

    RaycastHit2D Raycast(Vector2 offset, Vector2 rayDirection, float length, LayerMask layer)
    {
        Vector2 pos = transform.position;
        RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDirection, length, layer);

        Color color = hit ? Color.red : Color.green;

        Debug.DrawRay(pos + offset, rayDirection * length, color);
        return hit;
    }
}